package com.yunyao.web.utils;


import com.yunyao.common.core.redis.RedisCache;
import com.yunyao.common.web.util.SpringContextUtils;
import com.yunyao.common.dto.dict.DataDictionary;
import com.yunyao.framework.service.system.DataDictionaryService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.List;
import java.util.Map;

/**
 * 字典缓存工具类
 * @author chenyejian
 * @date 2024-07-21
 */
@Lazy
public class DictUtils{
    private static Logger logger = LoggerFactory.getLogger(DictUtils.class);
    @Autowired
    private static DataDictionaryService dataDictionaryService  = SpringContextUtils.getBean(DataDictionaryService.class);
    @Autowired
    private static RedisCache redisCache = SpringContextUtils.getBean(RedisCache.class);

    private static final String DICT_KEY_PREFIX = "dict:";

    public DictUtils() {
    }

    /**
     * 获取字典列表
     * @param groupCode
     * @return
     */
    public static List<DataDictionary> getDictList(String groupCode) {
        String reidsKey =DICT_KEY_PREFIX + groupCode.replace("_","").toUpperCase();
        List<DataDictionary> dictionaryList = null;
        try {
            if (redisCache.hasKey(reidsKey)) {
                dictionaryList = redisCache.getCacheList(reidsKey, DataDictionary.class);
                return dictionaryList;
            }
            dictionaryList = dataDictionaryService.getDictionaryList(groupCode);
            if (CollectionUtils.isEmpty(dictionaryList)) {
                redisCache.setCacheList(reidsKey, dictionaryList);
                return dictionaryList;
            }
        } catch (Exception e) {
            logger.warn("读取字典缓存失败!",e);
        }
        return dictionaryList;
    }

    /**
     * 设置字典列表到缓存
     * @param groupCode
     */
    public static void setDictionaryList2Cache(String groupCode) {
        List<DataDictionary> dictionaryList = dataDictionaryService.getDictionaryList(groupCode);
        if (dictionaryList != null && dictionaryList.size() > 0) {
            String reidsKey =DICT_KEY_PREFIX + groupCode.replace("_","").toUpperCase();
            clearDictionaryListFromCache(reidsKey);
            redisCache.setCacheList(reidsKey, dictionaryList);
        }
    }

    /**
     * 清除字典列表缓存
     * @param reidsKey
     * @return
     */
    public static Boolean clearDictionaryListFromCache(String reidsKey) {
//        String reidsKey =DICT_KEY_PREFIX + groupCode.replace("_","").toUpperCase();
        return redisCache.deleteObject(reidsKey);
    }

    /**
     * 初始化所有字典缓存
     */
    public static void initAllDictionaryListCache() {
        Map<String,List<DataDictionary>> dataDictionaryListMap = dataDictionaryService.getDictionaryListMap();
        if (null!=dataDictionaryListMap && dataDictionaryListMap.size()>0){
            dataDictionaryListMap.forEach((k,v)->{
                String reidsKey =DICT_KEY_PREFIX + k.replace("_","").toUpperCase();
                clearDictionaryListFromCache(reidsKey);
                redisCache.setCacheList(reidsKey,v);
            });
        }
    }

    /**
     * 根据字典组和字典key获取字典值
     * @param groupCode
     * @param dictKey
     * @return
     */
    public static String getDictValue(String groupCode, String dictKey) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList == null || dictionaryList.size() == 0) {
            /**
             * 字典不存在，返回当前值dictKey
             */
            return dictKey;
        }
        return dictionaryList.stream().filter(d -> d.getKey().equals(dictKey)).map(DataDictionary::getValue).findFirst().orElse("");
    }

    /**
     * 根据字典组和字典value获取字典值
     * @param groupCode
     * @param dictValue
     * @return
     */
    public static String getDictKey(String groupCode, String dictValue) {
        List<DataDictionary> dictionaryList = getDictList(groupCode);
        if (dictionaryList == null || dictionaryList.size() == 0) {
            /**
             * 字典不存在，返回当前值dictValue
             */
            return dictValue;
        }
        return dictionaryList.stream().filter(d -> d.getValue().equals(dictValue)).map(DataDictionary::getKey).findFirst().orElse("");
    }


}
